
package com.epam.android.training.level1.activity;

import com.epam.android.training.level1.R;

import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

/**
 * Base activity for application pages that hold standard options menu logic.
 * 
 * @author Anton Novikau
 */
public class BasePageActivity extends Activity implements IntentActions {

  /**
   * Initialize the contents of the Activity's standard options menu. Iflates
   * the menu content and disable the item that navigates to currently loaded
   * Activity.
   */
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main_menu, menu);

    // disable menu item of currently loaded activity
    String action = getIntent().getAction();
    if (ACTION_TESTS.equals(action)) {
      menu.findItem(R.id.tests_page).setEnabled(false);
    } else if (ACTION_NEWS.equals(action)) {
      menu.findItem(R.id.news_page).setEnabled(false);
    } else if (ACTION_SETTINGS.equals(action)) {
      menu.findItem(R.id.settings_page).setEnabled(false);
    } else if (ACTION_ABOUT.equals(action)) {
      menu.findItem(R.id.about_page).setEnabled(false);
    }

    return super.onCreateOptionsMenu(menu);
  }

  /**
   * This hook is called whenever an item in your options menu is selected. Each
   * options item starts particular Activity.
   */
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    boolean status;
    switch (item.getItemId()) {
      case R.id.tests_page:
        startActivity(new Intent(ACTION_TESTS));
        status = true;
        break;
      case R.id.news_page:
        startActivity(new Intent(ACTION_NEWS));
        status = true;
        break;
      case R.id.settings_page:
        startActivity(new Intent(ACTION_SETTINGS));
        status = true;
        break;
      case R.id.about_page:
        startActivity(new Intent(ACTION_ABOUT));
        status = true;
        break;
      default:
        status = super.onOptionsItemSelected(item);
        break;
    }
    return status;
  }
}
